<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Api;

use App\Models\Env\Host;
use App\Models\Env\HostEnv;
use App\Models\Module\ProductModule;
use App\Models\Projects\Product;
use App\Models\Projects\Project;
use App\Models\Projects\ProjectCate;
use App\Models\TestApi\TestParamRes;
use Carbon\Carbon;
use App\Models\Eloquent;

/**
 * Class QtpSampleValue
 *
 * @property int $id
 * @property int $api_id
 * @property int $param_id
 * @property string $sample_key
 * @property string $sample_rule
 * @property string $sample_value
 * @property int $value_type
 * @property string $remark
 * @property string $created_adminer
 * @property string $updated_adminer
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class SampleValue extends Eloquent
{
	protected $table = 'sample_value';

	protected $casts = [
		'api_id' => 'int',
        'param_id' => 'int',
		'value_type' => 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'api_id',
        'param_id',
		'sample_key',
		'sample_rule',
		'sample_value',
		'value_type',
		'remark',
		'created_adminer',
		'updated_adminer',
		'status'
	];

    // 0未取样 1取样成功 2取样失败
    const STATUS_INIT = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_FAIL = 2;
    public static $status_maps = [
        self::STATUS_INIT => '未取样',
        self::STATUS_SUCCESS => '取样成功',
        self::STATUS_FAIL => '取样失败',
    ];

    const VALUE_TYPE_INT = 1;
    const VALUE_TYPE_STRING = 2;
    public static $value_type_maps = [
        self::VALUE_TYPE_INT => 'Int',
        self::VALUE_TYPE_STRING => 'String'
    ];

    public static function getValue(string $sample_key)
    {
        $one = self::where('sample_key', $sample_key)->where('status',  self::STATUS_SUCCESS)->first();

        $res =  ['value' => $one->sample_value ?? "", 'type' => $one->value_type ?? ""];

        if($res['value']) {
            $value = $res['value'];
            if($res['type'] == self::VALUE_TYPE_INT) {
                $value = (int) $res['value'];
            }

            if($res['type'] == self::VALUE_TYPE_STRING) {
                $value = (string) $res['value'];
            }
        } else {
            $value = "";
        }

        return $value;
    }

    public static function getBYParamID($param_id)
    {
        return self::where("param_id", $param_id)->orderBy("id", "ASC")->get();
    }

    public function label()
    {
        $this->status_label = self::$status_maps[$this->status] ?? '';
        $this->value_type_label = self::$value_type_maps[$this->value_type] ?? '未知';
        $this->param = ApisParam::find($this->param_id)->param ?? '';
        if(!$api = Apis::find($this->api_id)) {
            return $this;
        }
        $this->headers = Apis::getHeaders($api) ?: [];
        list($this->host_env, $this->host_url) = ['', ''];
        if($api->host_id > 0) {
            $HostEnv = HostEnv::find($api->host_id);
            list($this->host_env, $this->host_url) = [
                $HostEnv->name ?? '',
                Host::where('host_env_id', $HostEnv->id)->where('enable', 1)->first()->http ?? ''
            ];
        }
        $this->cate_name = ProjectCate::find($api->cate_id)->name ?? '';
        $this->project_name = Project::find($api->project_id)->name ?? '';
        $this->product_name = Product::find($api->product_id)->name ?? '';
        $this->f_module_name = ProductModule::find($api->f_module_id)->name ?? '';
        $this->s_module_name = ProductModule::find($api->s_module_id)->name ?? '';
        $this->method_type_lable = Apis::$method_type_maps[$api->method_type] ?? '未知';
        $this->api = $api->api;
        $this->api_name = $api->name;
        $this->recently_res = TestParamRes::where('param_id', $this->param_id)->orderByDesc('id')->first()?->res ?? null; // 最近一次测试返回值
        return $this;
    }

    public static $fillables = [
        'api_id' => 'Api.ID',
        'param_id' => '参数ID',
        'sample_key' => '取样key命名',
        'sample_rule' => '取样规则',
        'sample_value' => '取样得到的值',
        'value_type' => '值得类型',
        'remark' => '说明',
        'created_adminer' => '创建人',
        'updated_adminer' => '更新人',
        'status' => '取样成功状态',
    ];
}
